import type { NextRequest } from 'next/server'
import type { PlayRecord } from '@/lib/types'

import { NextResponse } from 'next/server'
import { getAuthInfoFromCookie } from '@/lib/auth'
import { db } from '@/lib/db'

export const runtime = 'edge'

export async function GET(request: NextRequest) {
  try {
    // 从 cookie 获取用户信息
    const authInfo = getAuthInfoFromCookie(request)
    if (!authInfo || !authInfo.username) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }

    const records = await db.getAllPlayRecords(authInfo.username)
    return NextResponse.json(records, { status: 200 })
  }
  catch (err) {
    console.error('获取播放记录失败', err)
    return NextResponse.json(
      { error: 'Internal Server Error' },
      { status: 500 },
    )
  }
}

export async function POST(request: NextRequest) {
  try {
    // 从 cookie 获取用户信息
    const authInfo = getAuthInfoFromCookie(request)
    if (!authInfo || !authInfo.username) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }

    const body = await request.json()
    const { key, record }: { key: string, record: PlayRecord } = body

    if (!key || !record) {
      return NextResponse.json(
        { error: 'Missing key or record' },
        { status: 400 },
      )
    }

    // 验证播放记录数据
    if (!record.title || !record.source_name || record.index < 1) {
      return NextResponse.json(
        { error: 'Invalid record data' },
        { status: 400 },
      )
    }

    // 从key中解析source和id
    const [source, id] = key.split('+')
    if (!source || !id) {
      return NextResponse.json(
        { error: 'Invalid key format' },
        { status: 400 },
      )
    }

    const finalRecord = {
      ...record,
      save_time: record.save_time ?? Date.now(),
    } as PlayRecord

    await db.savePlayRecord(authInfo.username, source, id, finalRecord)

    return NextResponse.json({ success: true }, { status: 200 })
  }
  catch (err) {
    console.error('保存播放记录失败', err)
    return NextResponse.json(
      { error: 'Internal Server Error' },
      { status: 500 },
    )
  }
}

export async function DELETE(request: NextRequest) {
  try {
    // 从 cookie 获取用户信息
    const authInfo = getAuthInfoFromCookie(request)
    if (!authInfo || !authInfo.username) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
    }

    const username = authInfo.username
    const { searchParams } = new URL(request.url)
    const key = searchParams.get('key')

    if (key) {
      // 如果提供了 key，删除单条播放记录
      const [source, id] = key.split('+')
      if (!source || !id) {
        return NextResponse.json(
          { error: 'Invalid key format' },
          { status: 400 },
        )
      }

      await db.deletePlayRecord(username, source, id)
    }
    else {
      // 未提供 key，则清空全部播放记录
      // 目前 DbManager 没有对应方法，这里直接遍历删除
      const all = await db.getAllPlayRecords(username)
      await Promise.all(
        Object.keys(all).map(async (k) => {
          const [s, i] = k.split('+')
          if (s && i)
            await db.deletePlayRecord(username, s, i)
        }),
      )
    }

    return NextResponse.json({ success: true }, { status: 200 })
  }
  catch (err) {
    console.error('删除播放记录失败', err)
    return NextResponse.json(
      { error: 'Internal Server Error' },
      { status: 500 },
    )
  }
}
